# -*- coding:UTF-8 -*-

import RPi.GPIO as GPIO


class Motor(object):
    """
    Motor control
    """

    def __init__(self, ENA, AIN1, AIN2, ENB, BIN1, BIN2):
        GPIO.setup(ENA, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(AIN1, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(AIN2, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(ENB, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(BIN1, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(BIN2, GPIO.OUT, initial=GPIO.LOW)

        # 设置pwm引脚和频率为2000hz
        self.PWM_ENA = GPIO.PWM(ENA, 2000)
        self.PWM_ENB = GPIO.PWM(ENB, 2000)

        # 启动PWM设置占空比为100（0--100）
        self.PWM_ENA.start(0)
        self.PWM_ENB.start(0)

        self.leftBack = AIN1
        self.leftAhead = AIN2
        self.rightBack = BIN1
        self.rightAhead = BIN2

    def forward(self, speed=80):
        """
        前进
        :param speed: 电机速度
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.HIGH)
        GPIO.output(self.leftAhead, GPIO.LOW)
        GPIO.output(self.rightBack, GPIO.HIGH)
        GPIO.output(self.rightAhead, GPIO.LOW)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def back(self, speed=80):
        """
        后退
        :param speed: 电机速度
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.LOW)
        GPIO.output(self.leftAhead, GPIO.HIGH)
        GPIO.output(self.rightBack, GPIO.LOW)
        GPIO.output(self.rightAhead, GPIO.HIGH)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def right(self, speed=80):
        """
        右转
        :param speed: 电机速度
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.HIGH)
        GPIO.output(self.leftAhead, GPIO.LOW)
        GPIO.output(self.rightBack, GPIO.LOW)
        GPIO.output(self.rightAhead, GPIO.LOW)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def spin_right(self, speed=80):
        """
        原地右转
        :param speed: 电机速度
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.HIGH)
        GPIO.output(self.leftAhead, GPIO.LOW)
        GPIO.output(self.rightBack, GPIO.LOW)
        GPIO.output(self.rightAhead, GPIO.HIGH)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def left(self, speed=80):
        """
        左转
        :param speed: 电机速度
        :return:
        """
        GPIO.output(self.leftBack, GPIO.LOW)
        GPIO.output(self.leftAhead, GPIO.LOW)
        GPIO.output(self.rightBack, GPIO.HIGH)
        GPIO.output(self.rightAhead, GPIO.LOW)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def spin_left(self, speed=80):
        """
        原地左转
        :param speed: 电机速度
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.LOW)
        GPIO.output(self.leftAhead, GPIO.HIGH)
        GPIO.output(self.rightBack, GPIO.HIGH)
        GPIO.output(self.rightAhead, GPIO.LOW)
        self.PWM_ENA.ChangeDutyCycle(speed)
        self.PWM_ENB.ChangeDutyCycle(speed)

    def park(self):
        """
        停车
        :return: None
        """
        GPIO.output(self.leftBack, GPIO.LOW)
        GPIO.output(self.leftAhead, GPIO.LOW)
        GPIO.output(self.rightBack, GPIO.LOW)
        GPIO.output(self.rightAhead, GPIO.LOW)

    def brake(self):
        """
        刹车
        :return: None
        """
        self.PWM_ENA.ChangeDutyCycle(0)
        self.PWM_ENB.ChangeDutyCycle(0)

    def motorStop(self):
        """
        停止电机
        :return:
        """
        self.PWM_ENA.stop()
        self.PWM_ENB.stop()
